package newsDemo.controller;

import newsDemo.pojo.Result;
import newsDemo.pojo.user;
import newsDemo.service.userService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
public class userController {

    @Autowired
    private userService userService;

    @PostMapping("/signup")
    public Result signup(@RequestBody Map<String, Object> map) {
        String USER_NAME = (String) map.get("USER_NAME");
        String USER_PASSWORD = (String) map.get("USER_PASSWORD");
        if(USER_NAME == null || USER_NAME.length() < 6 || USER_PASSWORD == null || USER_PASSWORD.length() < 6
                || USER_NAME.length() > 24 || USER_PASSWORD.length() > 24
                || !USER_NAME.matches("^[a-zA-Z0-9]*$") || !USER_PASSWORD.matches("^[a-zA-Z0-9]*$")){
            return new Result(10001, null, "格式错误");
        }
        if(userService.select(USER_NAME) != null)  return new Result(10004, null, "昵称重复");

        userService.add(USER_NAME, USER_PASSWORD);
        user user = userService.select(USER_NAME);
        return Result.success(user);
    }

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, Object> map) {
        String USER_NAME = (String) map.get("USER_NAME");
        String USER_PASSWORD = (String) map.get("USER_PASSWORD");
        if(USER_NAME == null || USER_NAME.length() < 6 || USER_PASSWORD == null || USER_PASSWORD.length() < 6
                || USER_NAME.length() > 24 || USER_PASSWORD.length() > 24
                || !USER_NAME.matches("^[a-zA-Z0-9]*$") || !USER_PASSWORD.matches("^[a-zA-Z0-9]*$")){
            return new Result(10001, null, "格式错误");
        }
        user u = userService.select(USER_NAME);
        if(u == null || !u.getUserPassword().equals(USER_PASSWORD)){
            return new Result(10003, null, "账号或密码错误");
        }
        return Result.success(u);
    }

    @PostMapping("/updatePw")
    public Result updatePw(@RequestBody Map<String, Object> map) {
        String name = (String) map.get("USER_NAME");
        String newPw = (String) map.get("USER_NEW_PASSWORD");
        String oldPw = (String) map.get("USER_PASSWORD");
        if(name == null || name.length() < 6 || oldPw == null || oldPw.length() < 6 || newPw == null || newPw.length() < 6
                || name.length() > 24 || oldPw.length() > 24 || newPw.length() > 24
                || !name.matches("^[a-zA-Z0-9]*$") || !oldPw.matches("^[a-zA-Z0-9]*$") || !newPw.matches("^[a-zA-Z0-9]*$")){
            return new Result(10001, null, "格式错误");
        }
        user u = userService.select(name);
        if(u == null || !u.getUserPassword().equals(oldPw)){
            return new Result(10003, null, "账号或密码错误");
        }
        u.setUserPassword(newPw);
        userService.update(u);
        return Result.success();
    }

    @PostMapping("/updatePwByKey")
    public Result updatePwByKey(@RequestBody Map<String, Object> map) {
        String name = (String) map.get("USER_NAME");
        String newPw = (String) map.get("USER_NEW_PASSWORD");
        String key = (String) map.get("USER_KEY");
        if(name == null || name.length() < 6 || newPw == null || newPw.length() < 6
                || name.length() > 24 || newPw.length() > 24
                || !name.matches("^[a-zA-Z0-9]*$") || !newPw.matches("^[a-zA-Z0-9]*$")){
            return new Result(10001, null, "格式错误");
        }
        user u = userService.select(name);
        if(u == null || !u.getUserKey().equals(key)){
            return new Result(10003, null, "账号或验证码错误");
        }
        u.setUserPassword(newPw);
        userService.update(u);
        return Result.success();
    }

    @PostMapping("/del")
    public Result del(@RequestBody Map<String, Object> map) {
        String USER_NAME = (String) map.get("USER_NAME");
        String USER_PASSWORD = (String) map.get("USER_PASSWORD");
        if(USER_NAME == null || USER_NAME.length() < 6 || USER_PASSWORD == null || USER_PASSWORD.length() < 6
                || USER_NAME.length() > 24 || USER_PASSWORD.length() > 24
                || !USER_NAME.matches("^[a-zA-Z0-9]*$") || !USER_PASSWORD.matches("^[a-zA-Z0-9]*$")){
            return new Result(10001, null, "格式错误");
        }
        user u = userService.select(USER_NAME);
        if(u == null || !u.getUserPassword().equals(USER_PASSWORD)){
            return new Result(10003, null, "账号或密码错误");
        }
        userService.delete(u.getUserId());
        return Result.success();
    }
}
